package com.itheima.stock.controller;


import com.itheima.stock.pojo.domain.AddRoleDomain;
import com.itheima.stock.pojo.domain.EditRoleDomain;
import com.itheima.stock.pojo.domain.PermissionsDomain;
import com.itheima.stock.pojo.domain.SysUserDomain;
import com.itheima.stock.pojo.entity.SysRole;
import com.itheima.stock.pojo.vo.req.P;
import com.itheima.stock.pojo.vo.req.PageResult;
import com.itheima.stock.pojo.vo.req.UserPage;
import com.itheima.stock.pojo.vo.resp.PermissionsVO;
import com.itheima.stock.pojo.vo.resp.R;
import com.itheima.stock.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
public class RoleController {


    @Autowired
    private RoleService roleService;
    @PreAuthorize("hasAuthority('sys:role:list')")
    @PostMapping("/roles")
    public R<PageResult<SysRole>> RolePage(@RequestBody P p){
        return roleService.RolePage(p);
    }

    @GetMapping("/permissions/tree/all")
    public R<List<PermissionsVO>> getPermissions(){
        return roleService.getPermissions();
    }
    @PreAuthorize("hasAuthority('sys:role:add')")
    @PostMapping("/role")
    public R addRole(@RequestBody AddRoleDomain addRoleDomain){
        return roleService.addRole(addRoleDomain);
    }
    @GetMapping("/role/{roleId}")
    public R<List<String>> getRolePermission(@PathVariable String roleId){
        return roleService.getRolePermission(roleId);
    }
    @PreAuthorize("hasAuthority('sys:role:update')")
    @PutMapping("/role")
    public R editRole(@RequestBody EditRoleDomain editRoleDomain){
        return roleService.editRole(editRoleDomain);
    }
    @DeleteMapping("/role/{roleId}")
    @PreAuthorize("hasAuthority('sys:role:delete')")
    public R delRole(@PathVariable String roleId){
        return roleService.delRole(roleId);
    }
    @PostMapping("/role/{roleId}/{status}")
    @PreAuthorize("hasAuthority('sys:role:update')")
    public R editRoleStatus(@PathVariable("roleId") String roleId,@PathVariable("status") Integer status){
        return roleService.editRoleStatus(roleId,status);
    }
}
